{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 推导式\n",
    "一种数据结构构建另一种数据结构\n",
    "- 元组推导式\n",
    "- 列表推导式\n",
    "- 字典推导式\n",
    "- 集合推导式"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "<class 'generator'>\n"
     ]
    }
   ],
   "source": [
    "# 元组推导式\n",
    "# （out_exp_res for item in Sequence）\n",
    "t1 = (x for x in range(10))\n",
    "for i in t1:\n",
    "    print(i)\n",
    "print(type(t1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "# 列表推导式# 元组推导式\n",
    "# [out_exp_res for item in Sequence]\n",
    "t1 = [x for x in range(10)]\n",
    "for i in t1:\n",
    "    print(i)\n",
    "print(t1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "Bob:3\n",
      "Tom:3\n",
      "alice:5\n",
      "Jerry:5\n",
      "Wendy:5\n",
      "Smith:5\n"
     ]
    }
   ],
   "source": [
    "names = ['Bob','Tom','alice','Jerry','Wendy','Smith']\n",
    "# 将列表中各字符串值为键，各字符串的长度为值，组成键值对\n",
    "newdict = {name:len(name) for name in names}\n",
    "print(type(newdict))\n",
    "for k,v in newdict.items():\n",
    "    print(f'{k}:{v}')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'张三': 56, '李四': 67, '王五': 34, '赵六': 76}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 字典推导式   压缩\n",
    "name=('张三','李四','王五','赵六')\n",
    "score=(56,67,34,76)\n",
    "d = {k:v for k,v in zip(name,score)}\n",
    "print(d)\n",
    "print(type(d))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}